package com.bartender.consumer.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.bartender.consumer.domain.WxUser;
import com.bartender.consumer.service.IWxAuthService;
import com.bartender.consumer.service.IWxUserService;
import com.bartender.consumer.service.ITokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class WxAuthServiceImpl implements IWxAuthService {

    @Autowired
    private WxMaService wxMaService;

    @Autowired
    private IWxUserService wxUserService;

    @Autowired
    private ITokenService tokenService;

    @Override
    public Map<String, Object> login(String code) throws Exception {
        try {
            WxMaJscode2SessionResult session = wxMaService.getUserService().getSessionInfo(code);
            String openid = session.getOpenid();
            String sessionKey = session.getSessionKey();
            
            // 查询用户是否存在
            WxUser wxUser = new WxUser();
            wxUser.setOpenid(openid);
            WxUser existUser = wxUserService.selectWxUserList(wxUser).stream()
                .findFirst()
                .orElse(null);

            Map<String, Object> result = new HashMap<>();
            String token = tokenService.createToken(openid, sessionKey);
            result.put("token", token);
            result.put("isRegistered", existUser != null);
            if (existUser != null) {
                WxUser userInfo = wxUserService.selectWxUserByOpenid(openid);
                result.put("userInfo", userInfo);
            } else {
                wxUser.setCreateDate(new Date());
                wxUser.setStatus("0");
                wxUserService.insertWxUser(wxUser);
                WxUser userInfo = wxUserService.selectWxUserByOpenid(openid);
                result.put("userInfo", userInfo);
            }
            return result;
        } catch (Exception e) {
            throw new Exception("微信登录失败：" + e.getMessage());
        }
    }
//
//    @Override
//    public VxUser register(VxUser vxUser) throws Exception {
//        try {
//            // 检查必要参数
//            if (vxUser.getOpenid() == null || vxUser.getOpenid().trim().isEmpty()) {
//                throw new Exception("openid不能为空");
//            }
//
//            // 检查是否已注册
//            VxUser existUser = new VxUser();
//            existUser.setOpenid(vxUser.getOpenid());
//            if (!vxUserService.selectVxUserList(existUser).isEmpty()) {
//                throw new Exception("该用户已注册");
//            }
//
//            // 设置默认值
//            if (vxUser.getNickName() == null || vxUser.getNickName().trim().isEmpty()) {
//                vxUser.setNickName("微信用户");
//            }
//            vxUser.setCreateDate(new Date());
//            vxUser.setStatus("0"); // 0-正常 1-停用
//
//            // 打印调试信息
//            System.out.println("注册用户信息: " + vxUser.toString());
//
//            vxUserService.insertVxUser(vxUser);
//            return vxUser;
//        } catch (Exception e) {
//            throw new Exception("注册失败：" + e.getMessage());
//        }
//    }
}